﻿
using System;
using System.Collections.Generic;
namespace CharpClassGen
{
    //方法
    public class Method : Member
    {
        public List<string> codeBlock = new List<string>();
        public List<Argument> arguments = new List<Argument>();
        public Method SetExtendModifier(ExtendModifier extendModifier)
        {
            return SetExtendModifier(extendModifier.ToString().ToLower());
        }
        public Method SetExtendModifier(string extendModifier)
        {
            this.extendModifier = extendModifier;
            return this;
        }

        public Method AddCodeBlock(string codeBlock)
        {
            if (isAbstruct)
            {
                throw new Exception("抽象方法不能添加方法体");
            }
            this.codeBlock.Add(codeBlock);
            return this;
        }
        public Method AddArgument(Argument argument)
        {
            this.arguments.Add(argument);
            return this;
        }
        public Method(string type, string name, Accesser accesser = Accesser.Public, ExtendModifier extendModifier = ExtendModifier.none) : base(accesser, extendModifier, type, name)
        {

        }

        public override string ToString()
        {
            base.ToString();

            //添加参数
            sb.Append('(');

            for (int i = 0; i < arguments.Count; i++)
            {
                sb.Append(arguments[i].ToString());
                if (i != arguments.Count - 1)
                {
                    sb.Append(',');
                }
            }
            sb.Append(")");

            if (!isAbstruct)
            {
                //方法体
                sb.AppendLine($"\r\n{Helper.Retract(retrat)}{{");
                Helper.AddMuitLineToStringBuilder(sb,codeBlock,retrat+1);
                sb.AppendLine($"{Helper. Retract(retrat)}}}");
            }
            else
            {
                sb.AppendLine(";");
            }
            return sb.ToString();
        }
    }
}



